<?php
class picture
{ 
	var $type = 'jpg'; 
	var $width; 
	var $height; 
	var $srcimg; 
	var $x = 0;
	var $y = 0;
	var $w = 100;
	var $h = 100;
	var $dstimg; 
	// 临时创建的图象
	var $im; 
	var $wi = 224;
	var $wid = 0;
	var $hid = 0;

	function picture($img,  $dstimg='')
	{
		$this->srcimg = $img;
		$this->type = strtolower(substr(strrchr($this->srcimg, "."), 1)); 
		$this->initi_img(); 
		$this->dstimg = $dstimg;
		$this->width = imagesx($this->im);
		$this->height = imagesy($this->im); 
	}

	function resize(&$in)
	{ 
		$wi = imagesx($in);
		$hi = imagesy($in);
		if($this->wid > 0){//定高
			if($this->wid >= $hi) return $in;
			// 实际图象的比例
			$ratio = $wi / $hi;
			$out = imagecreatetruecolor($ratio * $this->wid, $this->wid);
			imagecopyresampled($out, $in, 0, 0, 0, 0, $ratio * $this->wid, $this->wid, $wi, $hi);
		}else{//定宽
			if($this->hid >= $wi) return $in;
			// 实际图象的比例
			$ratio = $wi / $hi;
			$out = imagecreatetruecolor($this->hid, $this->hid/$ratio);
			imagecopyresampled($out, $in, 0, 0, 0, 0, $this->hid,$this->hid/$ratio,  $wi, $hi);
		}
		return $out;
	} 

	// 初始化图象
	function initi_img()
	{
		if ($this->type == "jpg")
		{
			$this->im = imagecreatefromjpeg($this->srcimg);
		}
		if ($this->type == "gif")
		{
			$this->im = imagecreatefromgif($this->srcimg);
		}
		if ($this->type == "png")
		{
			$this->im = imagecreatefrompng($this->srcimg);
		}
	} 

	function setSize($wid, $hid = 0)
	{
		$this->wid = $wid;
		$this->hid = $hid;
	}

	function output(&$out, $weight = 70){
		ImageJpeg ($out, $this->dstimg, $weight); 
		ImageDestroy ($this->im);
		ImageDestroy ($out);
	}
}
class action
{
	function query_all($sql)
	{
		global $_SGLOBAL;
		$query = $_SGLOBAL['db']->query( $sql );
		$list = array();
		while($value = $_SGLOBAL['db']->fetch_array( $query )){
			$list[] = $value;
		}
		return $list;
	}

	function query_exec($sql, $type = false)
	{
		global $_SGLOBAL;
		$_SGLOBAL['db']->query( $sql);
		if($type == true) return $_SGLOBAL['db']->insert_id();
	}

	function query_one($sql)
	{
		global $_SGLOBAL;
		$query = $_SGLOBAL['db']->query( $sql );
		return $_SGLOBAL['db']->fetch_array( $query );
	}

	function upload()
	{
		global $_SC;
		include_once(S_ROOT.'./source/function_cp.php');
		$FILE = $_FILES['picture'];
		if(isset($FILE) && is_array($FILE) && $FILE['name'] && $FILE['error'] == 0)
		{
			$allowpictype = array('jpg','gif','png','jpeg');
			$FILE['size'] = intval($FILE['size']);
			if(empty($FILE['size']) || empty($FILE['tmp_name']) || !empty($FILE['error'])) return array('status'=>404, 'message'=>cplang('lack_of_access_to_upload_file_size'));
			$fileext = fileext($FILE['name']);
			if(!in_array($fileext, $allowpictype))  return array('status'=>404, 'message'=>cplang('only_allows_upload_file_types'));
			$new_name = 'action';
			$this->createdir(substr($_SC['attachdir'] . $new_name, 2));
			$new_name .=  '/' . $_SERVER['REQUEST_TIME'] . '_' . $this->random(5) .  '.' . $fileext;
			$tmp_name = $FILE['tmp_name'];
			if(@copy($tmp_name, $_SC['attachdir'].$new_name)) {
				@unlink($tmp_name);
			} elseif((function_exists('move_uploaded_file') && @move_uploaded_file($tmp_name, $_SC['attachdir'].$new_name))) {
			} elseif(@rename($tmp_name, $_SC['attachdir'].$new_name)) {

			} else {
				return array('status'=>404, 'message'=>cplang('mobile_picture_temporary_failure'));
			}

			//缩略图
			$thumb = $this->get_thumb($_SC['attachdir'].$new_name);
			$img = new picture($_SC['attachdir'].$new_name, $thumb);
			$img->setSize(0, 140);
			$out = $img->resize($img->im);
			$img->output($out);
			return array('status'=>200, 'message'=>array(substr($thumb,2), $fileext));
		}
		return false;
	}

	function random($length)
	{
		$chars = '012345678901234567895678901234542384098312302131231230';
		$max = strlen($chars);
		$hash = '';
		mt_srand((double)microtime() * 1000000);
		for($i = 0; $i < $length; $i ++){
			$hash .= $chars[mt_rand(0, $max)];
		}
		return $hash;
	}

	function get_thumb($img)
	{
		$ext = fileext($img);
		return $img . '_s.' . $ext ;
	}

	function createdir($dir)
    {
		return is_dir($dir) || ($this->createdir(dirname($dir)) && mkdir($dir));
    }

	function format_xinqi($x)
	{
		static  $xinqi = array('Sun'=>'星期天', 'Mon'=>'星期一', 'Tue'=>'星期二', 'Wed'=>'星期三', 'Thu'=>'星期四', 'Fri'=>'星期五', 'Sat'=>'星期六'); 
		return $xinqi[$x];
	}

	function get_event_status($id, $uid)
	{
		return $this->query_one("select * from ".tname("app_action_join")." where id = '{$id}' and uid = '{$uid}' ");
	}

	function format_form($id, &$event, &$status, $style = '')
	{
		$left = "<form id=\"joinform_{$id}\" name=\"joinform_{$id}\" action=\"action.php?ac=ajax&amp;op=join\" method=\"post\" class=\"quickpost\"><input type=\"hidden\" name=\"id\" value=\"{$id}\"/><input type=\"hidden\" name=\"jointype\" value=\"join\"/>";
		$number = " {$style} {$event['join_num']}人参加 {$event['interest_num']}人感兴趣";
		$right = " {$style} {$number}</form>";
		if($_SERVER['REQUEST_TIME'] < $event['start_time']){
			if(empty($status)){
				return "{$left}<input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='join';Form.submitDo('joinform_{$id}');\" class='submit' value='申请加入'/> <input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='interest';Form.submitDo('joinform_{$id}');\" class='button' value='我感兴趣'/>{$right}";
			}elseif($status['status'] == 1){
				return "【你已经退出】{$number}";
			}
			if($status['type'] == 'join'){
				if($status['is_pass'] == 0){
					return "【等待发起者的审核】{$number}";
				}else{
					return "{$left}<input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='quite';Form.submitDo('joinform_{$id}');\" class='button' value='不参加了'/>{$right}";
				}
			}else{
				return "{$left}【你对该活动感兴趣】<input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='join';Form.submitDo('joinform_{$id}');\" class='submit' value='申请加入'/> <input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='no_interest';Form.submitDo('joinform_{$id}');\" class='button' value='没兴趣了'/>{$right}";
			}
		}else{
			if($_SERVER['REQUEST_TIME'] > $event['end_time']){
				return "【该活动已经结束】{$number}";
			}else{
				return "【该活动已经开始了】{$number}";
			}
		}
	}

	function show_form($id, &$event, &$status)
	{
		$left = "<form id=\"joinform_{$id}\" name=\"joinform_{$id}\" action=\"action.php?ac=ajax&amp;op=join\" method=\"post\" class=\"quickpost\"><input type=\"hidden\" name=\"id\" value=\"{$id}\"/><input type=\"hidden\" name=\"jointype\" value=\"join\"/>";
		$number = "{$event['join_num']}人参加 {$event['interest_num']}人感兴趣";
		$right = "{$number}</form>";
		if($_SERVER['REQUEST_TIME'] < $event['start_time']){
			if(empty($status)){
				return "{$left}我的状态：没有关注<br /><br /><input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='join';Form.submitDo('joinform_{$id}');\" class='submit' value='申请加入'/><input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='interest';Form.submitDo('joinform_{$id}');\" class='submit' value='我感兴趣'/><br /><br />{$right}";
			}elseif($status['status'] == 1){
				return "我的状态：你已经退出<br /><br />{$number}";
			}
			if($status['type'] == 'join'){
				if($status['is_pass'] == 0){
					return "我的状态：等待发起者的审核<br /><br />{$number}";
				}else{
					return "{$left}我的状态：已经参加活动，通过审核<br /><br /><input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='quite';Form.submitDo('joinform_{$id}');\" class='button' value='不参加了'/><br /><br />{$right}";
				}
			}else{
				return "{$left}我的状态：我对该活动感兴趣<br /><br /><input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='join';Form.submitDo('joinform_{$id}');\" class='submit' value='申请加入'/> <input type='button' onclick=\"document.forms['joinform_{$id}'].jointype.value='no_interest';Form.submitDo('joinform_{$id}');\" class='submit' value='没兴趣了'/><br /><br />{$right}";
			}
		}else{
			if($_SERVER['REQUEST_TIME'] > $event['end_time']){
				return "活动状态：该活动已经结束<br /><br />{$number}";
			}else{
				return "活动状态：该活动已经开始了<br />{$number}";
			}
		}
	}
}
?>